import 'dart:io';
import '../extension/ex_safe.dart';
import './storage.dart';
import 'package:flutter/foundation.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:uuid/uuid.dart';

/**

  ///  description:  设备信息工具类
  使用示例
  void _getPackageInfo() async {
    PackageInfo packageInfo = await DeviceUtils.getPackageInfo();
    String appName = packageInfo.appName;
    String packageName = packageInfo.packageName;
    String version = packageInfo.version;
    String buildNumber = packageInfo.buildNumber;
    String buildSignature = packageInfo.buildSignature;
    String installerStore = packageInfo.installerStore ?? 'not available';
    print('packageInfo：');
    print('appName $appName');
    print('packageName $packageName');
    print('version $version');
    print('buildNumber $buildNumber');
    print('buildSignature $buildSignature');
    print('installerStore $installerStore');
  }

 void _getPackageInfo() async {
    String version = await DeviceUtils.version();
    print('app version = ：$version');
    setState(() {
      _currentVersion = version;
    });
  }

  void _getPackageInfo() {
    DeviceUtils.version().then((version) {
      print('app version = ：$version');
      setState(() {
        _currentVersion = version;
      });
    });
  }

*/ ///

class DeviceUtils {
  static bool get isDesktop => !isWeb && (isWindows || isLinux || isMacOS);

  static bool get isMobile => isAndroid || isIOS;

  static bool get isMobile2 =>
      defaultTargetPlatform == TargetPlatform.android ||
      defaultTargetPlatform == TargetPlatform.iOS;

  /// Platform不能在web端使用
  static bool get isWeb => kIsWeb;

  static bool get isWindows => isWeb ? false : Platform.isWindows;

  static bool get isLinux => isWeb ? false : Platform.isLinux;

  static bool get isMacOS => isWeb ? false : Platform.isMacOS;

  static bool get isAndroid => isWeb ? false : Platform.isAndroid;

  static bool get isFuchsia => isWeb ? false : Platform.isFuchsia;

  static bool get isIOS => isWeb ? false : Platform.isIOS;

  static Future<PackageInfo> getPackageInfo() async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    return packageInfo;
  }

  static Future<String> appName() async {
    PackageInfo packageInfo = await getPackageInfo();
    return packageInfo.appName;
  }

  static Future<String> packageName() async {
    PackageInfo packageInfo = await getPackageInfo();
    return packageInfo.packageName;
  }

  /// 版本号
  static Future<String> version() async {
    PackageInfo packageInfo = await getPackageInfo();
    return packageInfo.version;
  }

  /// 版本
  static Future<String> build() async {
    PackageInfo packageInfo = await getPackageInfo();
    return packageInfo.buildNumber;
  }

  /// 设备Id
  static Future<String> deviceId() async {
    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    String? deviceId;
    // 在移动平台（Android 或 iOS）
    if (Platform.isAndroid) {
      // 查询本地
      deviceId = Storage().getString('deviceId');
      if (deviceId.isEmpty) {
        // 新生成的uuid
        deviceId = const Uuid().v4();
        // 本地持久保存
        Storage().setString('deviceId', deviceId);
      }
      //读取安卓信息
      AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
      deviceId = '${androidInfo.id}@${deviceId.exStr}';
    } else if (Platform.isIOS) {
      IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
      deviceId = '${iosInfo.utsname.machine}@${iosInfo.identifierForVendor}';
    }
    return deviceId ?? '';
  }

  /// 设备系统信息
  static Future<String> systemVersion() async {
    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    String? systemVer;
    // 在移动平台（Android 或 iOS）
    if (Platform.isAndroid) {
      //读取安卓信息
      AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
      systemVer  = androidInfo.version.release;
    } else if (Platform.isIOS) {
      IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
      systemVer  = iosInfo.systemVersion;
    }
    return systemVer  ?? '';
  }

  /// 平台名称
  static Future<String> platform() async {
    if (kIsWeb) {
      return 'WEB';
    } else {
      // 在移动平台（Android 或 iOS）
      if (Platform.isAndroid) {
        return 'ANDROID';
      } else if (Platform.isIOS) {
        return 'IOS';
      }
    }
    return "";
  }

  static Future<String> buildSignature() async {
    PackageInfo packageInfo = await getPackageInfo();
    return packageInfo.buildSignature;
  }

  static Future<String?> installerStore() async {
    PackageInfo packageInfo = await getPackageInfo();
    return packageInfo.installerStore;
  }
}
